
package woaini.wind.modules.plugin.rest;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import woaini.wind.annotation.Log;
import woaini.wind.modules.plugin.domain.PluginInfo;
import woaini.wind.modules.plugin.service.PluginInfoService;
import woaini.wind.modules.plugin.service.dto.PluginInfoQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import jakarta.servlet.http.HttpServletResponse;
import woaini.wind.utils.PageResult;
import woaini.wind.modules.plugin.service.dto.PluginInfoDto;

/**

* @author yefeng
* @date 2024-11-05
**/
@RestController
@RequiredArgsConstructor
@Tag(name = "plugin管理")
@RequestMapping("/api/pluginInfo")
public class PluginInfoController {

    private final PluginInfoService pluginInfoService;

    @Log("导出数据")
    @Operation(summary = "导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('pluginInfo:list')")
    public void exportPluginInfo(HttpServletResponse response, PluginInfoQueryCriteria criteria) throws IOException {
        pluginInfoService.download(pluginInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询plugin")
    @Operation(summary = "查询plugin")
    @PreAuthorize("@el.check('pluginInfo:list')")
    public ResponseEntity<PageResult<PluginInfoDto>> queryPluginInfo(PluginInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(pluginInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增plugin")
    @Operation(summary = "新增plugin")
    @PreAuthorize("@el.check('pluginInfo:add')")
    public ResponseEntity<Object> createPluginInfo(@Validated @RequestBody PluginInfo resources){
        pluginInfoService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改plugin")
    @Operation(summary = "修改plugin")
    @PreAuthorize("@el.check('pluginInfo:edit')")
    public ResponseEntity<Object> updatePluginInfo(@Validated @RequestBody PluginInfo resources){
        pluginInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除plugin")
    @Operation(summary = "删除plugin")
    @PreAuthorize("@el.check('pluginInfo:del')")
    public ResponseEntity<Object> deletePluginInfo(@RequestBody String[] ids) {
        pluginInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}